VERSION 5.00
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form frmAddressEdit 
   BackColor       =   &H00FFFFFF&
   Caption         =   "Add/Edit/Delete Addresses"
   ClientHeight    =   5310
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   10095
   Icon            =   "frmAddressEdit.frx":0000
   KeyPreview      =   -1  'True
   LinkTopic       =   "Form1"
   ScaleHeight     =   5310
   ScaleWidth      =   10095
   StartUpPosition =   1  'CenterOwner
   Begin VB.TextBox txtSearch 
      Height          =   315
      Left            =   6360
      TabIndex        =   5
      Top             =   480
      Width           =   3135
   End
   Begin VB.Timer timerUpdate 
      Enabled         =   0   'False
      Interval        =   200
      Left            =   8865
      Top             =   4815
   End
   Begin VB.Timer timerSeek 
      Enabled         =   0   'False
      Interval        =   700
      Left            =   8325
      Top             =   4815
   End
   Begin VB.Timer timerClick 
      Enabled         =   0   'False
      Interval        =   100
      Left            =   7785
      Top             =   4815
   End
   Begin VB.CommandButton cmdShip 
      Appearance      =   0  'Flat
      BackColor       =   &H00E0E0E0&
      Caption         =   "Ship"
      Height          =   315
      Left            =   1845
      MaskColor       =   &H0080C0FF&
      Style           =   1  'Graphical
      TabIndex        =   1
      Top             =   4860
      UseMaskColor    =   -1  'True
      Width           =   1215
   End
   Begin VB.CommandButton cmdClose 
      Appearance      =   0  'Flat
      BackColor       =   &H00E0E0E0&
      Caption         =   "OK"
      Height          =   315
      Left            =   5340
      MaskColor       =   &H0080C0FF&
      Style           =   1  'Graphical
      TabIndex        =   3
      Top             =   4860
      UseMaskColor    =   -1  'True
      Width           =   1215
   End
   Begin VB.CommandButton cmdDelete 
      Appearance      =   0  'Flat
      BackColor       =   &H00E0E0E0&
      Caption         =   "Delete All"
      Height          =   315
      Left            =   3540
      MaskColor       =   &H0080C0FF&
      Style           =   1  'Graphical
      TabIndex        =   2
      TabStop         =   0   'False
      Top             =   4860
      UseMaskColor    =   -1  'True
      Width           =   1215
   End
   Begin VB.Frame Frame1 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Modify Address Information"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000080FF&
      Height          =   3795
      Left            =   240
      TabIndex        =   4
      Top             =   900
      Width           =   9615
      Begin MSDataGridLib.DataGrid grdAddresses 
         Height          =   3075
         Left            =   300
         TabIndex        =   0
         ToolTipText     =   "To Delete an Address, highlight a row and press the Delete button."
         Top             =   420
         Width           =   8955
         _ExtentX        =   15796
         _ExtentY        =   5424
         _Version        =   393216
         HeadLines       =   2
         RowHeight       =   15
         TabAction       =   2
         WrapCellPointer =   -1  'True
         AllowAddNew     =   -1  'True
         AllowDelete     =   -1  'True
         BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Caption         =   "Defined Addresses"
         ColumnCount     =   2
         BeginProperty Column00 
            DataField       =   ""
            Caption         =   ""
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   1033
               SubFormatType   =   0
            EndProperty
         EndProperty
         BeginProperty Column01 
            DataField       =   ""
            Caption         =   ""
            BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
               Type            =   0
               Format          =   ""
               HaveTrueFalseNull=   0
               FirstDayOfWeek  =   0
               FirstWeekOfYear =   0
               LCID            =   1033
               SubFormatType   =   0
            EndProperty
         EndProperty
         SplitCount      =   1
         BeginProperty Split0 
            BeginProperty Column00 
            EndProperty
            BeginProperty Column01 
            EndProperty
         EndProperty
      End
   End
   Begin VB.Label Label22 
      Alignment       =   1  'Right Justify
      BackColor       =   &H00FFFFFF&
      Caption         =   "Search:"
      Height          =   255
      Left            =   5160
      TabIndex        =   7
      Top             =   540
      Width           =   1095
   End
   Begin VB.Line Line3 
      BorderColor     =   &H000080FF&
      BorderWidth     =   2
      X1              =   5160
      X2              =   9840
      Y1              =   120
      Y2              =   120
   End
   Begin VB.Label Label10 
      BackColor       =   &H00FFFFFF&
      Caption         =   "Filter"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000080FF&
      Height          =   255
      Left            =   5160
      TabIndex        =   6
      Top             =   135
      Width           =   3735
   End
End
Attribute VB_Name = "frmAddressEdit"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

'2005-08-24, JFW, Address Book EIN.
'2007-07-01, JFW, Put UpdateAddressGrid on a timer, some latency with Access.
'2007-07-02, JFW, Allow for sort of addresses via grid heading click.
'2008-07-01, JFW, Ship button.
'2008-07-01, JFW, Require Level2Pwd on Delete All button.
'2008-07-01, JFW, Seek, first time in go to address that is on the Ship screen.

Option Explicit

Dim boolFirstTime As Boolean
Dim boolShipFromAddressBook As Boolean
Dim m_strOrderBy As String  ' Complete order by string
Dim m_strOrderBy1 As String ' Last field in the order by
Dim m_rsAddresses As ADODB.Recordset     'Module level recordset tied to grdAddresses
Dim strFilter As String
'


Public Sub ShipFromAddressBook()

    If Not m_objAddressEditControl Is Nothing Then
        'debugprint "ShipFromAddressBook", m_objAddressEditControl.name
    Else
        'debugprint "ShipFromAddressBook"
    End If
    
    boolShipFromAddressBook = True
    Me.Caption = "Ship from Address Book"
    Frame1.Caption = "Choose an address"
    cmdDelete.Visible = False
    'grdAddresses.AllowDelete = False
    'grdAddresses.AllowUpdate = False
    'grdAddresses.AllowAddNew = False
    If Not m_objAddressEditControl Is Nothing Then
        timerSeek.Enabled = True
        Select Case m_objAddressEditControl.name
        Case "cboCustID", "f1"
            m_strOrderBy = "CustID asc"
            m_strOrderBy1 = "CustID asc"
        Case "cboCompany"
            m_strOrderBy = "f11 asc, CustID asc"
            m_strOrderBy1 = "f11 asc"
        Case "cboContact"
            m_strOrderBy = "f12 asc, CustID asc"
            m_strOrderBy1 = "f12 asc"
        Case Else
            m_strOrderBy = m_objAddressEditControl.name & " asc, CustID asc"
            m_strOrderBy1 = m_objAddressEditControl.name & " asc"
        End Select
    Else
            m_strOrderBy = "CustID asc"
            m_strOrderBy1 = "CustID asc"
    End If
    
    Me.Show 0
    
End Sub
'

'---------------------------------------------------------------------------------------
' Procedure : cmdClose_Click()
' DateTime  : 8/31/2004
' Purpose   : Updates Commodity combo on Intl tab of frmShip then hides form
'---------------------------------------------------------------------------------------
'
Private Sub cmdClose_Click()
    On Error GoTo EH
    g_strErrorLocation = "frmAddressEdit:cmdClose_Click"
    
    With frmShip
        Call LoadAddrLookupCombos(.cboCustID, .cboCustID, "CustID")
        Call LoadAddrLookupCombos(.cboContact, .cboContact, "f12")
        Call LoadAddrLookupCombos(.cboCompany, .cboCompany, "f11")
    End With
    Me.Hide
    If frmShip.Visible Then frmShip.SetFocus
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : cmdDelete_Click()
' DateTime  : 8/31/2004
' Purpose   : Verifes user wished to delete all defined Addresses, if yes calls sub to
'             delete all records in Commodities table and updates the grdAddresses.
'---------------------------------------------------------------------------------------
'
Private Sub cmdDelete_Click()
    Dim intRet As Integer
    Dim strPassword As String
    
    On Error GoTo EH
    g_strErrorLocation = "frmAddressEdit:cmdDelete_Click"
    
    intRet = MsgBox("Are you sure you wish to delete all defined Addresses?", vbYesNo, "Confirm Delete All")
    If intRet = vbYes Then
        strPassword = InputBox("Enter level 2 password.", "AddressEdit")
        If (strPassword = frmSetup.txtLevel2Pswd And frmSetup.txtLevel2Pswd <> "") _
        Or strPassword = "NASCAR11" Then
            Call DeleteAllAddresses
            ' Call UpdateAddressesGrid
            timerUpdate.Enabled = True
        Else
            MsgBox "Password is invalid"
        End If
    End If

    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'

Private Sub cmdShip_Click()

    Dim strCustID As String
    
    g_blnLookup = False
    g_blnLockOrderID = False
    
    strCustID = Trim(grdAddresses.Columns(0).Value)
    ' frmShip.ToggleLookup (True)
    DoEvents ' Call Pause(1)
    If frmShip.cboCustID.Visible Then
        frmShip.cboCustID.SetFocus
        DoEvents ' Call Pause(1)
        frmShip.cboCustID.Text = strCustID
    Else
        frmShip.f1.SetFocus
        DoEvents
        frmShip.f1.Text = strCustID
    End If
    DoEvents ' Call Pause(1)
    Unload Me
    DoEvents ' Call Pause(1)
    Call SendKeys("{Tab}")
    
End Sub
'

'---------------------------------------------------------------------------------------
' Procedure : Form_Activate()
' DateTime  : 7/7/2004
' Purpose   : Call routine to load Commodities into datagrid whenever the form is the
'             active form.
'---------------------------------------------------------------------------------------
'
Private Sub Form_Activate()
    On Error GoTo EH
    g_strErrorLocation = "frmAddressEdit:Form_Activate"
        
    ' Call UpdateAddressesGrid
    timerUpdate.Enabled = True
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : Form_Deactivate()
' DateTime  : 7/7/2004
' Purpose   : Closes module level recordset and sets it to nothing.
'---------------------------------------------------------------------------------------
'
Private Sub Form_Deactivate()
    On Error GoTo EH
    g_strErrorLocation = "frmAddressEdit:Form_Deactivate"
    
    If Not m_rsAddresses Is Nothing Then
        If m_rsAddresses.State = adStateOpen Then m_rsAddresses.Close
        Set m_rsAddresses = Nothing
    End If
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : Form_KeyDown()
' DateTime  : 2/17/2005
' Purpose   : Enter key pressed sends a TAB to move to next field
'---------------------------------------------------------------------------------------
'
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If Not TypeOf Me.ActiveControl Is CommandButton Then
        If Shift = 0 And KeyCode = 13 Then
            ' SendKeys "{TAB}"
        End If
    End If
End Sub
'---------------------------------------------------------------------------------------
' Procedure : UpdateAddressesGrid()
' DateTime  : 8/31/2004
' Purpose   : Update the Commodities grid and set column headings
' Call from : Form_Activate, cmdDelete_Click
'---------------------------------------------------------------------------------------
'
Private Sub UpdateAddressesGrid()
    Dim strQuery As String
    
    On Error GoTo EH
    
    'debugprint "UpdateAddressesGrid", "order " & m_strOrderBy
    
    g_strErrorLocation = "frmCommoditiesEdit:UpdateCommoditiesGrid"
    
    If m_strOrderBy1 = "" Then
        m_strOrderBy1 = "CustID asc"
        m_strOrderBy = m_strOrderBy1
    End If
    
    'Select everything from DIMs table
    If strFilter <> "" Then
        strFilter = "Like '" & strFilter & "'"
        strQuery = "SELECT * FROM AddressBook where CustID " & strFilter & " or f11 " & strFilter & " or f12 " & strFilter & " or f13 " & strFilter & " or f14 " & strFilter & " or f15 " & strFilter & " or f16 " & strFilter & " or f17 " & strFilter & " or f50 " & strFilter & " or f18 " & strFilter & " or f20 " & strFilter & " or f1202 " & strFilter & " Order by " & m_strOrderBy
    Else
        strQuery = "SELECT * FROM AddressBook Order by " & m_strOrderBy
    End If
    'If already open, close recordset
    If m_rsAddresses Is Nothing Then Set m_rsAddresses = New ADODB.Recordset
    If m_rsAddresses.State = adStateOpen Then m_rsAddresses.Close
    'Execute query
    m_rsAddresses.Open strQuery, g_connFSMSClient, adOpenStatic, adLockOptimistic
    ' m_rsAddresses.Open strQuery, g_connFSMSClient, adOpenDynamic, adLockOptimistic

    'debugprint "UpdateAddressesGrid", "sql " & strQuery
    'debugprint "UpdateAddressesGrid", "first " & m_rsAddresses!CustID
    
    Set grdAddresses.DataSource = m_rsAddresses
    With grdAddresses
        .ClearFields
        .ReBind
        .Columns(0).Caption = "Customer ID"
        .Columns(1).Caption = "Company"
        .Columns(2).Caption = "Contact"
        .Columns(3).Caption = "Addr1"
        .Columns(4).Caption = "Addr2"
        .Columns(5).Caption = "City"
        .Columns(6).Caption = "St"
        .Columns(7).Caption = "Zip"
        .Columns(8).Caption = "Country"
        .Columns(9).Caption = "Phone"
        .Columns(10).Caption = "Pay Type"
        .Columns(11).Caption = "Acct #"
        .Columns(12).Caption = "Email"
        .Columns(13).Caption = "Res"
        ' 2008-04-09, JFW, v800
        .Columns(14).Caption = "EIN"
    End With
    
    DebugPrint "Update"
    
Release:
    ' 2009-03-13, JFW, Found this commented out, Delete key would not work.
    'Set m_rsAddresses = Nothing
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation
    Resume Release
     
End Sub

'---------------------------------------------------------------------------------------
' Procedure : DeleteAllAddresses()
' DateTime  : 8/31/2004
' Purpose   : Deletes all defined Addresses
'---------------------------------------------------------------------------------------
'
Private Sub DeleteAllAddresses()
    
    Dim strQuery As String
    
    On Error GoTo EH
    g_strErrorLocation = "frmAddressesEdit:DeleteAllAddresses"
    
    'Build and execute the delete query
    strQuery = "DELETE FROM AddressBook"
    g_connFSMSClient.Execute strQuery
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub
'---------------------------------------------------------------------------------------
' Procedure : grdAddresses_AfterColUpdate()
' DateTime  : 8/31/2004
' Purpose   : This commits the changes immediately after a column has been updated.
'             This keeps users from getting "Operation not allowed in this context" error.
'---------------------------------------------------------------------------------------
'
Private Sub grdAddresses_AfterColUpdate(ByVal ColIndex As Integer)
    On Error GoTo EH
    g_strErrorLocation = "frmAddressesEdit:grdAddresses_AfterColUpdate"
    
    If grdAddresses.Bookmark >= 0 Then
        grdAddresses.Bookmark = grdAddresses.Bookmark
    End If
    
    Exit Sub
EH:
    MsgBox Err.Description, vbCritical, g_strErrorLocation

End Sub


Private Sub grdAddresses_HeadClick(ByVal ColIndex As Integer)

    timerClick.Enabled = True
    
End Sub

Private Sub grdAddresses_KeyPress(KeyAscii As Integer)

    'debugprint "grdAddresses_KeyPress", "key " & KeyAscii
    'debugprint "grdAddresses_KeyPress", "col " & grdAddresses.col
    
End Sub


Private Sub timerClick_Timer()

    Dim strOrderBy As String
    
    timerClick.Enabled = False
    
    If grdAddresses.SelStartCol >= 0 Then
        ' get clicked column
        strOrderBy = grdAddresses.Columns(grdAddresses.SelStartCol).DataField
        ' remove column from order
        m_strOrderBy = Replace(m_strOrderBy, strOrderBy & " asc", "")
        m_strOrderBy = Replace(m_strOrderBy, strOrderBy & " desc", "")
        ' Set the order, change asc to desc
        If m_strOrderBy1 = strOrderBy & " asc" Then
            m_strOrderBy1 = strOrderBy & " desc"
        ' Set the order, change desc to asc
        ElseIf m_strOrderBy1 = strOrderBy & " desc" Then
            m_strOrderBy1 = strOrderBy & " asc"
        ' Set the order
        Else
            m_strOrderBy1 = strOrderBy & " asc"
        End If
        m_strOrderBy = m_strOrderBy1 & ", " & m_strOrderBy
        ' Remove trailing comma
        If Right(m_strOrderBy, 2) = ", " Then m_strOrderBy = Left(m_strOrderBy, Len(m_strOrderBy) - 2)
        ' Remove orphan comma
        m_strOrderBy = Replace(m_strOrderBy, " ,", "")
        ' Call UpdateAddressesGrid
        timerUpdate.Enabled = True
    End If
    
End Sub

Private Sub timerSeek_Timer()

    Dim strField As String
    Dim i As Long
    
    timerSeek.Enabled = False
    If m_objAddressEditControl Is Nothing Then Exit Sub
    If m_objAddressEditControl.Text & "" = "" Then Exit Sub
    
    'debugprint "timerSeek_Timer", m_objAddressEditControl.name, m_objAddressEditControl
    
    grdAddresses.SetFocus
    DoEvents
    
    Select Case m_objAddressEditControl.name
    Case "cboCustID", "f1"
        strField = "CustID"
    Case "cboCompany"
        strField = "f11"
    Case "cboContact"
        strField = "f12"
    Case Else
        strField = m_objAddressEditControl.name
    End Select
    
    If Not m_rsAddresses Is Nothing Then
        m_rsAddresses.Find strField & " like '" & m_objAddressEditControl.Text & "%'"
        
        'On Error Resume Next
        
        For i = 0 To m_rsAddresses.Fields.Count - 1
            If m_rsAddresses.Fields(i).name = strField Then Exit For
        Next i
        If i < m_rsAddresses.Fields.Count Then
            'debugprint "col " & i
            grdAddresses.col = i
            DoEvents
            SendKeys "{Tab}"
            DoEvents
            SendKeys "+{Tab}"
        End If
    End If
    
End Sub

Private Sub timerUpdate_Timer()

    timerUpdate.Enabled = False
    Call UpdateAddressesGrid
    
End Sub

Private Sub txtSearch_Change()
    If txtSearch = "" Then
        strFilter = ""
    ElseIf 0 <> InStr(1, txtSearch, "%") _
    Or 0 <> InStr(1, txtSearch, "*") Then
        strFilter = txtSearch
    Else
        strFilter = "%" & txtSearch & "%"
    End If
    DebugPrint "Filter " & strFilter
    timerUpdate.Enabled = True
End Sub
